<?php

error_reporting (E_ALL);
ini_set('display_errors',TRUE);

	class fluxInsert {

		private $table       = NULL;
		private $values      = NULL;
		private $mainObj     = NULL;
		private $fieldsCache = array();
		private $cache       = FALSE;
		private $autoFilter  = TRUE;

		public function __construct ($link,$method,$fields,$mainObj){

			$this->mainObj=$mainObj;	
			$this->setTable($fields[0]);

		}

		public function setCache ($state=FALSE){  //En realidad este deberia ser un metodo de 
				  												//$this->mainObj
			if($state)
					  $this->cache=TRUE;

		}

		private function setTable ($table=NULL) {

			if (is_null($table)||empty($table))
				$this->mainObj->throwError('Unable to set an empty table for doing an insert');

			$this->table=$table;

		}

		public function into () {
		}

		public function values ($args){
			
			if (!is_array ($args))
					  $this->mainObj->throwError('You should use an array for using this method as in field => value'); 

			while (list($k,$v) = each ($args)) {

				if (!$this->fieldExists($k))
					$this->fieldDoesntExists($k);

				$this->addField ($k,$v);

			}

		}


		public function dumpValues () {

				  echo 'INSERT VALUES'."\n".
						 '-------------'."\n";

				  var_dump($this->values);

		}

		public function dumpTable () {

				  echo 'INSERT TABLE '."\n".
						 '-------------'."\n";

				  var_dump($this->table);

				  echo "\n";

		}

		private function addField($field,$value=NULL) {

			if (is_null($field))
					  $this->mainObj->throwError('A field must be specified when inserting');

			if(is_array($value))
					  $value=$value[0];

			$this->values[]=array(
					  'field'=>$field,
					  'value'=>is_null($value) ? NULL : $value
					);

		}

		//So we dont have to query the database each time when verifying a field

		private function setFieldsInCache (){

			if (!sizeof($this->fieldsCache))
			  $this->fieldsCache=$this->mainObj->getFieldsFrom($this->table);

		}

		private function fieldDoesntExists ($field){

			$this->mainObj->throwError($field . ' is not a valid field or a valid method in table ' . $this->table);

		}

		private function fieldExists ($field=NULL) {

			if ($this->cache) {

				$this->setFieldsInCache();

				foreach ($this->fieldsCache as $k=>$v) { 
					if ($v['Field'] == $field)
						return $v;
				}

				return FALSE;

			}

			foreach ($this->mainObj->getFieldsFrom($this->table) as $k=>$v)
					if ($v['Field'] == $field)
						return $v;

			return FALSE;

		}

		public function getFieldProperties($field){
				  return $this->fieldExists($field);
		}

		public function __call ($field,$value){

			if ($this->fieldExists($field)&&!method_exists($this,'___'.$field.'___')){
					  $this->addField($field,$value);
					  return;
			}

			$this->fieldDoesntExists($field);

		}

		public function getAutoFilterState () {
				  return $this->autoFilter;
		}

		public function setAutoFilter ($state) {
				 $this->autoFilter=$state;
		}

		private function fieldCheck($field,$value){

			$fProperties=$this->getFieldProperties($field);

			if ($fProperties['Key'] == 'PRI'){
					  $msg =  'You cannot use ->' . $field . '<- for inserting ' .
								 'the given field is a PRIMARY KEY' . "\n";
						
					  die ($msg);

			}

			if ($fProperties['Type'] == 'INT' && is_numeric($value))
					  return FALSE;

			return TRUE;


		}

		public function __toString () {

				$right = NULL;
				$left  = NULL;

				foreach ($this->values as $k => $v){

						$left  .= $v['field'].',';

						$quoteValue=$this->fieldCheck ($v['field'],$v['value']);

						if ($this->getAutoFilterState()) {

							if ($quoteValue)	
									  $right .="'". mysql_real_escape_string($v['value'],$this->mainObj->getLink()) . "'". ',';
							else
									  $right .= mysql_real_escape_string($v['value'],$this->mainObj->getLink()) .',';
							continue;

						}
						 

						$this->mainObj->writeWarning('Auto filter of fields to be inserted is OFF!');

							if ($quoteValue)	
									  $right .="'".$v['value']."'".',';
							else
									  $right .=$v['value'] .',';

				  }

				  $right = substr($right,0,-1);
				  $left  = substr($left,0,-1);

				  $query = 'INSERT INTO ' . $this->table . ' ('.$left.')' . 
							  ' VALUES ' . 
							  '(' . $right . ')' . "\n";

				  return $query;

		}

		public function execute() {

				  $this->mainObj->doQuery($this->__toString());
				  return mysql_insert_id($this->mainObj->getLink());

		}

	}

?>
